//
//  UITextFieldController.swift
//  iPhone
//
//  Created by Toj on 3/27/23.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class UITextFieldController: STBaseViewController {
    
    private lazy var cardIDTextField: FMTextField = {
        let view = setupTextField()
        let maxCharCount = 0
        view.maxCharCount = maxCharCount
        // view.charFormatType = .threeFourFour
        // view.style = .titleLeft
        // view.leftMargin = 5
        
        //view.titleLabel.text = "卡号:"
        view.textField.returnKeyType = .next
        view.textField.keyboardType = .namePhonePad
        let ptext = "namePhonePad"
        let attributedPlaceholder = ptext.to_attributedString(font:  UIFont.systemFont(ofSize: 15), color: "#AAAAAA".to_color)
        view.textField.attributedPlaceholder = attributedPlaceholder
        
        let rightView = UIButton()
        rightView.adjustsImageWhenHighlighted = false
        rightView.setImage("commodity_tv_card_question".to_image, for: .normal)
        rightView.addTarget(self, action: #selector(didTapCardQuestion), for: .touchUpInside)
        rightView.frame.size = CGSize(width: 24, height: 24)
        
        view.rightView = rightView
//        view.backgroundColor = .random
        return view
    }()
    
    private lazy var cardIDTextField2: FMTextField = {
        let view = setupTextField()
        let maxCharCount = 11
        view.maxCharCount = maxCharCount
        view.charFormatType = .threeFourFour
        view.style = .titleLeft
        // view.leftMargin = 5
        
       // view.titleLabel.text = "卡号:"
        view.textField.returnKeyType = .next
        view.textField.keyboardType = .numberPad
        let ptext = "123456789"
        let attributedPlaceholder = ptext.to_attributedString(font:  UIFont.systemFont(ofSize: 15), color: "#AAAAAA".to_color)
        view.textField.attributedPlaceholder = attributedPlaceholder
        
        let rightView = UIButton()
        rightView.adjustsImageWhenHighlighted = false
        rightView.setImage("commodity_tv_card_question".to_image, for: .normal)
        rightView.addTarget(self, action: #selector(didTapCardQuestion), for: .touchUpInside)
        rightView.frame.size = CGSize(width: 24, height: 24)
        
        view.rightView = rightView
//        view.backgroundColor = .random
        return view
    }()
    
    private func setupTextField() -> FMTextField {
        let view = FMTextField()
        view.delegate = self
        
        view.titleNormalColor = "#AAAAAA".to_color
        view.titleHighlightColor = .blue
        view.titleLabel.font = UIFont.systemFont(ofSize: 12.f_scale)
        
        view.textField.clearButtonMode = .whileEditing // 清空按钮
        view.textField.font = UIFont.systemFont(ofSize: 18.f_scale)
        view.textField.textColor = "#333333".to_color
        
        view.maxCharLabel.textColor = "#999999".to_color
        view.maxCharLabel.font = UIFont.systemFont(ofSize: 12.f_scale)
        
        view.lineNormalColor = .gray
        view.lineHighlightColor = .blue
        
        return view
    }
    
    private lazy var textField: UITextField = {
        let view = UITextField()
        view.backgroundColor = .random
        return view
    }()
}

// MARK: - 生命周期
extension UITextFieldController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
        setupMakeInitialize()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
}

private extension UITextFieldController {
    
    @objc func didTapCardQuestion() {
        print("-->toj: func: \(#function)")
    }
}

// MARK: - 代理事件
extension UITextFieldController: FMTextFieldDelegate {
    
    func textFieldEditingChange(_ view: FMTextField, text: String, stringValue: String) {
        
        print("-->toj: text: \(text), stringValue: \(stringValue)")
    }
}

// MARK: - 初始化
extension UITextFieldController {
    
    private func setupMakeAddSubviews() {
        view.addSubview(cardIDTextField)
        view.addSubview(cardIDTextField2)
        view.addSubview(textField)
    }
    
    private func setupMakeLayoutSubviews() {
        cardIDTextField.snp.makeConstraints { make in
            make.top.equalTo(112.ui_scale)
            make.left.right.equalTo(view).inset(16)
            make.height.equalTo(80)
        }
        
        cardIDTextField2.snp.makeConstraints { make in
            make.top.equalTo(cardIDTextField.snp.bottom).offset(20)
            make.left.right.equalTo(view).inset(16)
            make.height.equalTo(80)
        }
        
        textField.snp.makeConstraints { make in
            make.top.equalTo(cardIDTextField2.snp.bottom).offset(20)
            make.left.right.equalTo(view).inset(16)
            make.height.equalTo(80)
        }
    }
    
    private func setupMakeInitialize() {
        view.backgroundColor = .white
    }
}
